package com.hsjk.szwj.module.pay.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsjk.szwj.module.pay.dal.dataobject.order.TransferOrderDO;
import com.hsjk.szwj.module.pay.dal.mysql.ITransferOrderMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author chenjiawei
 * @desc 转账订单表 服务实现类
 * @Date 2023/08/25 18:27
 **/
@Service
public class TransferOrderService extends ServiceImpl<ITransferOrderMapper, TransferOrderDO> {

    /**
     * 更新转账订单状态  【转账订单生成】 --》 【转账中】
     **/
    public boolean updateInit2Ing(Long transferId) {

        TransferOrderDO updateRecord = new TransferOrderDO();
        updateRecord.setState(TransferOrderDO.STATE_ING);

        return update(updateRecord, new LambdaUpdateWrapper<TransferOrderDO>()
                .eq(TransferOrderDO::getId, transferId)
                .eq(TransferOrderDO::getState, TransferOrderDO.STATE_INIT));
    }


    /**
     * 更新转账订单状态  【转账中】 --》 【转账成功】
     **/
    @Transactional
    public boolean updateIng2Success(Long transferId, String channelOrderNo) {

        TransferOrderDO updateRecord = new TransferOrderDO();
        updateRecord.setState(TransferOrderDO.STATE_SUCCESS);
        updateRecord.setChannelOrderNo(channelOrderNo);
        updateRecord.setSuccessTime(LocalDateTime.now());

        //更新转账订单表数据
        if (!update(updateRecord, new LambdaUpdateWrapper<TransferOrderDO>()
                .eq(TransferOrderDO::getId, transferId)
                .eq(TransferOrderDO::getState, TransferOrderDO.STATE_ING))
        ) {
            return false;
        }

        return true;
    }


    /**
     * 更新转账订单状态  【转账中】 --》 【转账失败】
     **/
    @Transactional
    public boolean updateIng2Fail(Long transferId,
                                  String channelOrderNo,
                                  String channelErrCode, String channelErrMsg) {

        TransferOrderDO updateRecord = new TransferOrderDO();
        updateRecord.setState(TransferOrderDO.STATE_FAIL);
        updateRecord.setErrCode(channelErrCode);
        updateRecord.setErrMsg(channelErrMsg);
        updateRecord.setChannelOrderNo(channelOrderNo);

        return update(updateRecord, new LambdaUpdateWrapper<TransferOrderDO>()
                .eq(TransferOrderDO::getId, transferId)
                .eq(TransferOrderDO::getState, TransferOrderDO.STATE_ING));
    }


    /**
     * 更新转账订单状态  【转账中】 --》 【转账成功/转账失败】
     **/
    @Transactional
    public boolean updateIng2SuccessOrFail(Long transferId, Integer updateState,
                                           String channelOrderNo, String channelErrCode,
                                           String channelErrMsg) {

        if (updateState == TransferOrderDO.STATE_ING) {
            return true;
        } else if (updateState == TransferOrderDO.STATE_SUCCESS) {
            return updateIng2Success(transferId, channelOrderNo);
        } else if (updateState == TransferOrderDO.STATE_FAIL) {
            return updateIng2Fail(transferId, channelOrderNo, channelErrCode, channelErrMsg);
        }
        return false;
    }


    /**
     * 查询商户订单
     **/
    public TransferOrderDO queryMchOrder(Long mchId, Long mchOrderId, Long transferId) {

        if (ObjectUtil.isNotNull(transferId)) {
            return getOne(new LambdaQueryWrapper<TransferOrderDO>()
                    .eq(TransferOrderDO::getMchId, mchId)
                    .eq(TransferOrderDO::getId, transferId));
        } else if (ObjectUtil.isNotNull(mchOrderId)) {
            return getOne(new LambdaQueryWrapper<TransferOrderDO>()
                    .eq(TransferOrderDO::getMchId, mchId)
                    .eq(TransferOrderDO::getMchOrderId, mchOrderId));
        } else {
            return null;
        }
    }


    public IPage<TransferOrderDO> pageList(IPage iPage,
                                           LambdaQueryWrapper<TransferOrderDO> wrapper,
                                           TransferOrderDO transferOrder, JSONObject paramJSON) {
        if (ObjectUtil.isNotNull(transferOrder.getId())) {
            wrapper.eq(TransferOrderDO::getId, transferOrder.getId());
        }
        if (ObjectUtil.isNotNull(transferOrder.getMchOrderId())) {
            wrapper.eq(TransferOrderDO::getMchOrderId, transferOrder.getMchOrderId());
        }
        if (StringUtils.isNotEmpty(transferOrder.getChannelOrderNo())) {
            wrapper.eq(TransferOrderDO::getChannelOrderNo, transferOrder.getChannelOrderNo());
        }
        if (ObjectUtil.isNotNull(transferOrder.getMchId())) {
            wrapper.eq(TransferOrderDO::getMchId, transferOrder.getMchId());
        }
        if (transferOrder.getState() != null) {
            wrapper.eq(TransferOrderDO::getState, transferOrder.getState());
        }
        if (ObjectUtil.isNotNull(transferOrder.getAppId())) {
            wrapper.eq(TransferOrderDO::getAppId, transferOrder.getAppId());
        }
        if (paramJSON != null) {
            if (StringUtils.isNotEmpty(paramJSON.getStr("createdStart"))) {
                wrapper.ge(TransferOrderDO::getCreateTime,
                        paramJSON.getStr("createdStart"));
            }
            if (StringUtils.isNotEmpty(paramJSON.getStr("createdEnd"))) {
                wrapper.le(TransferOrderDO::getCreateTime,
                        paramJSON.getStr("createdEnd"));
            }
        }
        // 三合一订单
        if (paramJSON != null
                && StringUtils.isNotEmpty(paramJSON.getStr("unionOrderId"))) {
            wrapper.and(wr -> {
                wr.eq(TransferOrderDO::getId, paramJSON.getStr("unionOrderId"))
                        .or().eq(TransferOrderDO::getMchOrderId, paramJSON.getStr("unionOrderId"))
                        .or().eq(TransferOrderDO::getChannelOrderNo, paramJSON.getStr("unionOrderId"));
            });
        }
        wrapper.orderByDesc(TransferOrderDO::getCreateTime);

        return page(iPage, wrapper);
    }
}
